package com.yunfeng.supercall.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.MainActivity;

/**
 * 实现了一个简单的标题栏，提供左侧返回按钮， 右侧菜单和中间的标题栏
 * 
 * @author 洋
 * 
 */
public class TitleBarView extends FrameLayout {

	private String mTitle, mRightText;
	private Drawable mRightRes;
	private TextView mTextViewTitle, mTextViewRight;

	private ImageView mImageViewRight, mImageViewLeft;

	@SuppressLint("NewApi")
	public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr,
			int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		init(context, attrs);
	}

	public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs);
	}

	public TitleBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public TitleBarView(Context context) {
		super(context);
		init(context, null);
	}

	private void init(Context context, AttributeSet attrs) {
		LayoutInflater.from(context).inflate(R.layout.base_title_bar, this,
				true);
		if (isInEditMode()) {
			return;
		}
		mTextViewTitle = (TextView) findViewById(R.id.title);
		mTextViewRight = (TextView) findViewById(R.id.right_text);
		mImageViewRight = (ImageView) findViewById(R.id.right);
		mImageViewLeft = (ImageView) findViewById(R.id.back_button);
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.TitleBarView, 0, 0);
		mTitle = a.getString(R.styleable.TitleBarView_mainTitle);
		mRightText = a.getString(R.styleable.TitleBarView_rightText);
		mRightRes = a.getDrawable(R.styleable.TitleBarView_rightIcon);
		int b = a.getResourceId(R.styleable.TitleBarView_android_background, 0);
		if (b != 0) {
			setBackgroundResource(b);
		} else {
			setBackgroundResource(R.color.base_orange);
		}
		Boolean show = a.getBoolean(R.styleable.TitleBarView_showBack, true);
		mImageViewLeft.setVisibility(show ? View.VISIBLE : View.INVISIBLE);
		if (getContext() instanceof MainActivity) {
			mImageViewLeft.setVisibility(View.INVISIBLE);
		}
		mImageViewLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				((Activity) getContext()).finish();
			}
		});
		a.recycle();
		if (!TextUtils.isEmpty(mTitle)) {
			mTextViewTitle.setText(mTitle);
		}
		if (!TextUtils.isEmpty(mRightText)) {
			mTextViewRight.setText(mRightText);
			mTextViewRight.setVisibility(View.VISIBLE);
		} else {
			// mTextViewRight.setVisibility(View.GONE);
		}
		if (mRightRes != null) {
			mImageViewRight.setImageDrawable(mRightRes);
		}
		setId(R.id.action_bar);
	}
}
